import axios from 'axios'
import api from '../../api/api'

const state = {//数据
  orderList:[],
  params:{
    total:0,
    pageSize: 10,
    query:'',
    productId:0,
    startDate: '',
    currentPage:0
  },
}

const getters = {//获取数据
  /*orderList: function (state) {
    return state.orderList
  }*/
  getOrderList: state => state.orderList,//es6箭头函数写法
  getOrderListParams: state => state.params
}

const mutations = {//对本地无需ajax的更改，使用mutations
  updateOrderList(state,changed){
    state.orderList = changed
  },
  updateParams(state,{key,val}){
    state.params[key] = val
    //console.log({key,val})
  }
}

const actions = {//action好处适合异步操作，action先执与后端交互，mutations后执行
  fetchOrderList({commit,state}){
    axios.post(api.getOrderListURL, state.params)
      .then((res)=>{
        commit('updateOrderList',res.data.list)
        commit('updateParams',{key:'total',val:res.data.total})
      })
      .catch((res)=>{
        console.log(res)
      })
  }
}

export default {
  state,
  getters,
  mutations,
  actions
}
